/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package unplag.preprocessor;

import unplag.core.Preprocessor;

/**
 *
 * @author Administrator
 */

public class StringPreprocessor implements Preprocessor{

    @Override
    public String process(String input) {
        if(input==null)
            return null;
        int len=input.length();
        boolean isInString=false;
        boolean isInCommentS=false;
        boolean isInCommentM=false;
        StringBuilder sb=new StringBuilder();

        for(int i=0;i<len;i++){
            char c          = input.charAt(i);
            char cBefore    = i==0      ?0:input.charAt(i-1);
            char cAfter     = i==len-1    ?0:input.charAt(i+1);
                    
            if(!isInCommentS && !isInCommentM && c=='\"' && cBefore!='\\'){
                isInString=!isInString;
                if(isInString)
                    sb.append('\"');
            }
            
            if(!isInString){
                if(!isInCommentS && !isInCommentM){
                    if(c=='/' && cAfter=='/')
                        isInCommentS=true;
                    if(c=='/' && cAfter=='*'){
                        isInCommentM=true;
                    }
                }else{
                    if(isInCommentS && c=='\n')
                        isInCommentS=false;
                    if(isInCommentM && c=='/' && cBefore=='*'){
                        isInCommentM=false;
                    }
                }

            }

            if(!isInString)
                sb.append(c);
        }

        return sb.toString();
    }
}
